const db = require("../../db/index");

exports.create = (req, res) => {
  const cellInfo = req.body;

  if (!cellInfo.type || !cellInfo.ownername) {
    return res.send({
      status: 1,
      message: "生产单元的种类或拥有者不能为空",
    });
  }

  let sql_query =
    "select * from cell_list where `type` = ? and `ownername` = ? ";
  db.query(sql_query, [cellInfo.type, cellInfo.ownername], (err, result) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    if (result.length > 0) {
      return res.send({
        status: 1,
        message: "生产单元已存在",
      });
    }
    let sql_insert = "insert into cell_list set ? ";
    db.query(
      sql_insert,
      {
        type: cellInfo.type,
        ownername: cellInfo.ownername,
        number: cellInfo.number,
      },
      (err, result) => {
        if (err) {
          return res.send({
            status: 2,
            message: err.message,
          });
        }
        if (result.affectedRows !== 1) {
          return res.send({
            status: 1,
            message: "生产单元添加失败，请稍后再试！",
          });
        }
        res.send({ status: 0, message: "添加成功！" });
      }
    );
  });
};

exports.deleteByCellId = (req, res) => {
  const cell_id = req.query.cell_id;

  if (!cell_id) {
    return res.send({
      status: 1,
      message: "生产单元id缺失",
    });
  }

  let sql_query = "select * from cell_list where id = ?";
  db.query(sql_query, cell_id, (err, result) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    if (result.length == 0) {
      return res.send({
        status: 1,
        message: "删除单元不存在",
      });
    }

    let sql_delete = "delete from cell_list where id = ?";
    db.query(sql_delete, cell_id, (err, result) => {
      if (err) {
        return res.send({
          status: 2,
          message: err.message,
        });
      }
      if (result.affectedRows != 1) {
        return res.send({
          status: 1,
          message: "删除失败，请稍后重试!",
        });
      }
      return res.send({
        status: 0,
        message: "删除成功",
      });
    });
  });
};
exports.updateByCellId = (req, res) => {
  const cellInfo = req.body;
  if (!cellInfo.cellId) {
    return res.send({
      status: 1,
      message: "生产单元id缺失",
    });
  }

  let sql_query = "select * from cell_list where id = ?";
  db.query(sql_query, cellInfo.cellId, (err, result) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    if (result.length == 0) {
      return res.send({
        status: 1,
        message: "修改单元不存在",
      });
    }

    let sql_update = "update cell_list set ? where id = ?";
    db.query(
      sql_update,
      [
        {
          type: cellInfo.type,
          ownername: cellInfo.ownername,
          number: cellInfo.number,
        },
        cellInfo.cellId,
      ],
      (err, result) => {
        if (err) {
          return res.send({
            status: 2,
            message: err.message,
          });
        }
        if (result.affectedRows != 1) {
          return res.send({
            status: 1,
            message: "修改失败，请稍后重试!",
          });
        }
        return res.send({
          status: 0,
          message: "修改成功",
        });
      }
    );
  });
};

exports.getAll = (req, res) => {
  let sql_query = "select * from cell_list limit ?";
  db.query(sql_query, 3, (err, result) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    return res.send({
      status: 0,
      total: result.length,
      message: "获取成功",
      data: result,
    });
  });
};
exports.getByCellId = (req, res) => {
  const cellId = req.query.cellId;
  if (!cellId) {
    return res.send({
      status: 1,
      message: "生产单元id缺失",
    });
  }
  let sql_query = "select * from cell_list where id =?";
  db.query(sql_query, cellId, (err, result) => {
    if (err) {
      return res.send({
        status: 2,
        message: err.message,
      });
    }
    return res.send({
      status: 0,
      message: "获取成功",
      data: result,
    });
  });
};
